//
//  DeckViewController.m
//  AnimatedFlashcards
//
//  Created by Timothy Costa on 10/4/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "DeckViewController.h"
#import "DeckCell.h"
#import "DeckCellFormatter.h"


@implementation DeckViewController

@synthesize fileDisplay;
@synthesize fileContents;

-(void)awakeFromNib
{
	fileContents = @" ";
	// set the custom cell as the table view's cell class
	DeckCell * aCustomCell = [[[DeckCell alloc] init] autorelease];
	[[decksTableView tableColumnWithIdentifier:@"name"] setDataCell:aCustomCell];
}

- (IBAction) addNewCardUsingTextFields:(id)sender
{
	Deck * selectedDeck = [[deck selectedObjects] lastObject];
	if (selectedDeck == nil) return;
	
	Card * newCard = [selectedDeck addNewCard];
	[deck_cards setSelectedObjects:[NSArray arrayWithObject:newCard]];
	
	newCard.side1 = [NSString stringWithString:[addSide1 stringValue]];
	newCard.side2 = [NSString stringWithString:[addSide2 stringValue]];
	newCard.hint = [NSString stringWithString:[addHint stringValue]];
	
	[addSide1 setStringValue:@""];
	[addSide2 setStringValue:@""];
	[addHint setStringValue:@""];

	[[addSide1 window] makeFirstResponder:addSide1];
}


- (IBAction) addNewCardToSelectedDeck: (id)sender
{
	Deck * selectedDeck = [[deck selectedObjects] lastObject];
	if (selectedDeck == nil) return;
	
	[selectedDeck addNewCard];
}
- (IBAction) removeSelectedCardFromSelectedDeck:(id)sender
{
	Deck * selectedDeck = [[deck selectedObjects] lastObject];
	Card * selectedCard = [[deck_cards selectedObjects] lastObject];
	if (selectedDeck == nil || selectedCard == nil) return;

	[selectedCard removeDecksObject:selectedDeck];
	[selectedDeck removeCardsObject:selectedCard];
}
- (IBAction) importCardsFromFile:(id)sender
{
	// Check if a deck is selected
	Deck * selectedDeck = [[deck selectedObjects] lastObject];
	if (selectedDeck == nil) return;
	
	// Deck is selected, open a file
	NSOpenPanel * openPanel = [NSOpenPanel openPanel];
	[openPanel setTitle:@"Choose file to import from."];
	[openPanel setCanChooseDirectories:NO];
	[openPanel setAllowsMultipleSelection:NO];
	
	if ([openPanel runModal] == NSOKButton) {
		NSURL * url = [[openPanel URLs]objectAtIndex:0];
		fileContents = [NSString stringWithContentsOfURL:url
												encoding:NSUTF8StringEncoding
												   error:nil];
		
		// Save the bad lines here
		NSMutableDictionary * failedCards = [[NSMutableDictionary alloc]init];
		// Save the bad lines with index
		NSUInteger index = 1;
		
		// File is opened.  Parse it into newLine characters and tabs
		NSArray * separatedCards = [fileContents componentsSeparatedByString:@"\n"];
		for (NSString *currentElement in separatedCards){	
			
			// remove whitespace from ends of element
			NSString * element = [currentElement stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
			
			// Create array of 0+ attributes
			NSArray * separatedAttributes = [element componentsSeparatedByString:@"\t"];
			// At least two attributes are needed for a card to be meaningful
			if (separatedAttributes.count >=2){
				Card * newCard = [selectedDeck addNewCard];
				// Set attributes eliminating whitespace at front and back
				newCard.side1 = [[separatedAttributes objectAtIndex:0]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
				newCard.side2 = [[separatedAttributes objectAtIndex:1]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
				newCard.hint = separatedAttributes.count >= 3 ? 
												  [[separatedAttributes objectAtIndex:2]stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]]
												: @"";
			} else {
				// Catch this to present to the user later
				[failedCards setObject:element forKey:[NSNumber numberWithInt:index]];
				NSLog(@"Item on line #%u of %u has only %u items.",index, separatedCards.count, separatedAttributes.count);
			}
			index++;
		}		
	}
}

- (NSArray*) currentSelections
{
	return [deck_cards selectedObjects];
}

@end
